package com.ibuildmap;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;

/**
 * Created by youkai on 2017/5/24.
 */
public class CustomOverLayMgt {

    TextureMapView mMapView;
    DataEntity mEntity;

    public CustomOverLayMgt(TextureMapView mapView, DataEntity entity) {
        mMapView = mapView;
        mEntity = entity;
    }

    public void draw() {
        LatLng latLng = new LatLng(mEntity.getLocationY(), mEntity.getLocationX());

        if (mEntity.isProject()) {
            drawProject(latLng);
        } else {
            drawArea(latLng);
        }
    }

    public void drawNav() {
        LatLng latLng = new LatLng(mEntity.getLocationY(), mEntity.getLocationX());
        Bundle bundle = new Bundle();
        bundle.putSerializable("entity", mEntity);
        View view = (View) LayoutInflater.from(mMapView.getContext())
                .inflate(R.layout.view_overlay_nav_project, null);
        TextView tv_projectName, tv_projectIcon;
        tv_projectName = (TextView) view.findViewById(R.id.tv_projectName);
        tv_projectIcon = (TextView) view.findViewById(R.id.tv_projectIcon);
        if (mEntity.getProjectLevel() == 1) {
            tv_projectIcon.setBackgroundResource(R.drawable.import_building);
        } else {
            tv_projectIcon.setBackgroundResource(R.drawable.building);
        }
        tv_projectName.setText(mEntity.getProjectName());

        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
                .fromView(view);
        MarkerOptions markerOptions = new MarkerOptions().position(latLng).icon(bitmapDescriptor)
                .perspective(false).zIndex(10).extraInfo(bundle);
        mMapView.getMap().addOverlay(markerOptions);
    }

    private void drawArea(LatLng center) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("entity", mEntity);

        View areaView = LayoutInflater.from(mMapView.getContext()).inflate(
                R.layout.view_overlay_area, null);

        TextView tvAreaName, tvProjectCount;
        tvAreaName = (TextView) areaView.findViewById(R.id.tv_area_name);
        tvProjectCount = (TextView) areaView.findViewById(R.id.tv_project_count);
        tvAreaName.setText(mEntity.getAreaName());
        tvProjectCount.setText(String.valueOf(mEntity.getProjectCount()));

        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
                .fromView(areaView);

        MarkerOptions markerOptions = new MarkerOptions().position(center).icon(bitmapDescriptor)
                .perspective(false).anchor(0.5f, 0.5f).zIndex(10).extraInfo(bundle);
        mMapView.getMap().addOverlay(markerOptions);
    }

    public void drawProject(LatLng center) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("entity", mEntity);
        View view = (View) LayoutInflater.from(mMapView.getContext())
                .inflate(R.layout.view_overlay_area_text, null);
        TextView tv_projectName, tv_projectIcon;
        tv_projectName = (TextView) view.findViewById(R.id.tv_projectName);
        tv_projectIcon = (TextView) view.findViewById(R.id.tv_projectIcon);
        if (mEntity.getProjectLevel() == 1) {
            tv_projectIcon.setBackgroundResource(R.drawable.import_building);
        } else {
            tv_projectIcon.setBackgroundResource(R.drawable.building);
        }
        tv_projectName.setText(mEntity.getProjectName());

        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
                .fromView(view);
        MarkerOptions markerOptions = new MarkerOptions().position(center).icon(bitmapDescriptor)
                .perspective(false).zIndex(10).extraInfo(bundle);
        mMapView.getMap().addOverlay(markerOptions);
    }
}
